<?php

require_once(SITE_MODULES_DIR . '/contact/ContactWebModule.php');

class SchoolcontactWebModule extends ContactWebModule {

    protected function getPersonlistFromEip($schid, $type = 'sch') {
        $user = $this->getUser();
        $controller = DataModel::factory('EipDataModel', array('RETRIEVER_CLASS' => 'UrlEipRetriever', 'PARSER_CLASS' => 'JSONDataParser', 'BASE_URL' => '[YOUR_SERVER]'));

        return $controller;
    }

    protected function initializeForPage() {
        $user = $this->getUser();
        $identity = $user->getAttribute('identity');
        $position = $user->getAttribute('position');
        $unit = $user->getAttribute('unit');
        $unitid = $user->getAttribute('unitid');

        switch($this->page) {
            case 'index':
                $this->redirectTo('detail');
                break;
            case 'detail':
                $pushButton = false;
                if ($unit == '教育處' || $position == '校長') {
                    $pushButton = true;
                } elseif ($identity == '教師' || $identity == '學生') {
                } else {
                    $this->unauthorizedAccess();
                }

                $type = 'sch';
                if ($unit == '教育處') {
                    $unitid = '029999';
                    $type = 'edu';
                }

                $personDetails = array();
                $controller = $this->getPersonlistFromEip($unitid, $type);
                $items = $controller->items();
                foreach($items as $item) {
                    extract($item);
                    $personDetails[] = array(
                        array('label' => '職稱', 'title' => $teajob),
                        array('label' => '姓名', 'title' => $teaname),
                        array('label' => '學校電話', 'title' => $schphone, 'url' => PhoneFormatter::getPhoneURL($schphone)),
                        array('label' => '個人手機', 'title' => $teamobile, 'url' => PhoneFormatter::getPhoneURL($teamobile)),
                        array('label' => '電子郵件', 'title' => $teaemail, 'url' => $this->buildMailToLink($teaemail)),
                    );
                }

                $this->assign('schoolName', $unit);
                $this->assign('pushButton', $pushButton);
                $this->assign('personDetails', $personDetails);
                break;
            case 'push':
                if ($unit <> '教育處' && $position <> '校長') {
                    $this->unauthorizedAccess();
                }
                break;
            case 'result':
                if ($unit <> '教育處' && $position <> '校長') {
                    $this->unauthorizedAccess();
                }
                $schname = $unit;
                $msg = $this->getArg('msg', '');
                $result = $this->pushMsg($schname, $msg);
                break;
        }
    }
}
